import type { FormRules } from "element-plus";
import { isPhone, isEmail } from "@pureadmin/utils";
import { matchPasswd } from "@/utils/reg";
import { $t, transformI18n } from "@/plugins/i18n";

/** 自定义表单规则校验 */
export const formRules = reactive(<FormRules>{
  nickName: [{ required: true, message: "用户昵称为必填项", trigger: "blur" }],
  userName: [{ required: true, message: "用户账号为必填项", trigger: "blur" }],
  password: [
    {
      required: true,
      message: transformI18n($t("login.purePassWordReg")),
      trigger: "blur"
    },
    {
      min: 6,
      max: 18,
      message: transformI18n($t("login.purePassWordRuleReg")),
      trigger: "blur"
    },
    {
      validator: (rule, value, callback) => {
        matchPasswd(value, 2)
          ? callback()
          : callback(new Error(transformI18n($t("login.purePassWordRuleReg"))));
      },
      trigger: "blur"
    }
  ],
  phone: [
    {
      validator: (rule, value, callback) => {
        if (value === "") {
          callback();
        } else if (!isPhone(value)) {
          callback(new Error(transformI18n($t("login.purePhoneCorrectReg"))));
        } else {
          callback();
        }
      },
      trigger: "blur"
      // trigger: "click" // 如果想在点击确定按钮时触发这个校验，trigger 设置成 click 即可
    }
  ],
  email: [
    {
      validator: (rule, value, callback) => {
        if (value === "") {
          callback();
        } else if (!isEmail(value)) {
          callback(new Error(transformI18n($t("login.pureEmailCorrectReg"))));
        } else {
          callback();
        }
      },
      trigger: "blur"
    }
  ]
});
